import {createRouter, createWebHistory} from 'vue-router';
import {qiankunWindow} from "vite-plugin-qiankun/dist/helper";
import {getAccessToken} from '@/utils/auth';
const { wsCache } = useCache();

// import { websocketStoreWithOut } from '@/store/modules/websocket';
// websocketStoreWithOut().connectWebSocket();
const router = createRouter({
  	history: createWebHistory(qiankunWindow.__POWERED_BY_QIANKUN__ ? `/${import.meta.env.VITE_APP_NAME}` : "/"),
	routes: [
		{
			path: "/login", 
			name: "login", 
			component: () => import("@/views/login/index.vue"),
			meta: { title: '登录', hidden: true },
		},
		{
			path: "/index",
			name: "index",
			component: () => import("@/views/msg/index.vue"),
			meta: { title: '消息', icon: 'EachApp_xiaoxi', type: 'msg' },
		},
		// {
		// 	path: '/test',
		// 	name: 'test',
		// 	component: () => import('@/views/test/index.vue'),
		// 	meta: { title: '测试', icon: 'EachApp_xiaoxi' }
		// },
		{
			path: '/:catchAll(.*)',
			name: "404",
			component: () => import('../App.vue')//这个是我自己的路径
		},
	]
})


// 路由加载前
router.beforeEach(async (to, from, next) => {
	if (to.meta.title) {
		window.document.title = `Each-${to.meta.title}`;
	}
	const isMain = wsCache.get('isMain');
	if(!isMain){
		if (getAccessToken()) {
			const is404 = to.matched?.filter(item => item.name !== '404');
			if (['/', '/login'].includes(to.path) || !is404.length) {
				next({ path: '/index' });
			}else {
				next();
			}
		} else {
			if (to.path === '/login') {
				next();
			} else {
				next(`/login`)
			}
		}
	}else{
		next();
	}
});

export default router
